//
//  UgcDetailCummentHeadCell_TokenCell.m
//  ZITHER
//
//  Created by weixin on 2019/4/16.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "UgcDetailCummentHeadCell_TokenCell.h"
#import "Ugc_Detail_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"
#import "MainFirst_Magazine_VC_WX.h"
#define kGAP 10
#define kAvatar_Size 40
@implementation UgcDetailCummentHeadCell_TokenCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    
    [self setUI];
    [self setMasonry];
}


-(void)setModelComment:(UgcCommentListModelSub *)modelComment{
    _modelComment = modelComment;
    
    
    self.nameUser.text = modelComment.nickName;
    [self.picUser sd_setImage:modelComment.avatar];
    self.comment.text = modelComment.commentContent;
    
    
    if ([modelComment.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
        self.followBtn.hidden = YES;
    }else{
        self.followBtn.hidden = NO;
    }
    
    
    
    /**
     
     [self addSubview:self.comment];
     [self addSubview:self.picUser];
     [self addSubview:self.nameUser];
     [self addSubview:self.viewLine];
     [self addSubview:self.supportLab];
     [self addSubview:self.unsupportedLab];
     [self addSubview:self.followBtn];
     [self addSubview:self.moreBtn];
     [self addSubview:self.tierLab];
     
     */
    
  

    
    
    //    self.comment.text = [NSString stringWithFormat:@"%@:%@",modelComment.nickName,modelComment.commentContent];
    //    if ([modelComment.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
    //        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
    //            //            make.bottom.equalTo(self).offset(-20);
    //            make.height.mas_equalTo(20);
    //        }];
    //        self.userDelete.hidden = NO;
    //    }else{
    //        self.userDelete.hidden = YES;
    //        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
    //            make.height.mas_equalTo(0);
    //            //            make.bottom.equalTo(self).offset(-12);
    //        }];
    //    }
    [self setNeedsLayout];
    //    [self configComment];
}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    self.tierLab.text = [NSString stringWithFormat:@"#%ld",indexPath.row];
}
-(void)setMasonry{
    
    [self.nameUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self.picUser.mas_right).offset(15);
        make.height.mas_equalTo(13);

    }];
    
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameUser.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(50);

    }];
    
    
    
    [self.picUser mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(15);
        make.height.width.mas_equalTo(30);
        //        make.bottom.equalTo(self).offset(-8);
    }];
    
    [self.tierLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.picUser.mas_bottom).offset(0);
        make.left.equalTo(self.picUser.mas_right).offset(15);
        make.height.mas_equalTo(12);
    }];
    
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tierLab.mas_bottom).offset(5);
        make.left.equalTo(self.picUser.mas_right).offset(10);
        make.right.equalTo(self).offset(-14);
        make.bottom.equalTo(self.mas_bottom).offset(-45);
    }];
    
    [self.supportLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comment.mas_bottom).offset(15);
        make.left.equalTo(self.picUser.mas_right).offset(15);
        make.height.width.mas_equalTo(15);
    }];
    
    [self.unsupportedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comment.mas_bottom).offset(15);
        make.left.equalTo(self.supportLab.mas_right).offset(30);
        make.height.width.mas_equalTo(15);
    }];
    
    [self.share mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comment.mas_bottom).offset(15);
        make.left.equalTo(self.unsupportedLab.mas_right).offset(30);
        make.height.width.mas_equalTo(15);
    }];
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comment.mas_bottom).offset(15);
        make.right.equalTo(self).offset(-18);
        make.height.width.mas_equalTo(15);
    }];
    
    
    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.picUser.mas_right).offset(10);
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(1);
    }];
    
    //    [self.userDelete mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(self.comment.mas_bottom).offset(5);
    //        make.left.equalTo(self).offset(14);
    //        make.bottom.equalTo(self).offset(-20);
    //    }];
    
}

-(void)setUI{
    [self addSubview:self.comment];
    [self addSubview:self.picUser];
    [self addSubview:self.nameUser];
    [self addSubview:self.viewLine];
    [self addSubview:self.supportLab];
    [self addSubview:self.unsupportedLab];
    [self addSubview:self.followBtn];
    [self addSubview:self.moreBtn];
    [self addSubview:self.tierLab];


    //    [self addSubview:self.userDelete];
}


-(void)configComment{
    
    NSString *str  = nil;
    str= [NSString stringWithFormat:@"%@  %@",
          self.modelComment.nickName,self.modelComment.commentContent];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
    
    if ([text.string isMoreThanOneLineWithSize:CGSizeMake(kScreenW-kGAP-kAvatar_Size-2*kGAP, CGFLOAT_MAX) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13] lineSpaceing:5.0]) {//margin
        muStyle.lineSpacing = 5.0;//设置行间距离
    }else{
        muStyle.lineSpacing = CGFLOAT_MIN;//设置行间距离
    }
    
    [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];
    
    [text addAttribute:NSForegroundColorAttributeName
                 value:[UIColor blackColor]
                 range:NSMakeRange(0, self.modelComment.nickName.length)];
    [text addAttribute:NSFontAttributeName
                 value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]
                 range:NSMakeRange(0, self.modelComment.nickName.length)];
    
    
    self.comment.attributedText = text;
    @weakify(self);
    [self.comment yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        @strongify(self);
        Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
        vc.userName = self.modelComment.nickName;
        vc.userId = self.modelComment.userId;
        vc.userAvatar = self.modelComment.avatar;
        [self.getNav pushViewController:vc animated:YES];
    }];
    //设置是否有点击效果，默认是YES
    self.comment.enabledTapEffect = NO;
    
    
}



-(BaseLab *)comment{
    if (!_comment) {
        _comment = [BaseLab initCustomLab:@"用户名称" color:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0] font:[UIFont fontWithName:@"PingFang-SC-Regular" size:14]];
        _comment.numberOfLines = 0;
        _comment.backgroundColor = [UIColor clearColor];


//        _comment = [[BaseLab alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _comment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
//        _comment.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0];
        //        _comment.text = @"一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复";
    }
    return _comment;
}


-(UIView *)viewLine{
    if (!_viewLine) {
        _viewLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _viewLine.backgroundColor = [UIColor colorWithHex:@"eeeeee"];
    }
    return _viewLine;
}

-(BaseImageV *)picUser{
    if (!_picUser) {
        _picUser = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        @weakify(self);
        [_picUser setRound:15];
        [[_picUser addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
    
    
    }
    return _picUser;
}

-(BaseLab *)nameUser{
    
    if (!_nameUser) {
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
            _nameUser = [BaseLab initCustomLab:@"用户名称" color:Main_Token_Model_MANAGER.backgroundColor font:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]];
        }else{
            _nameUser = [BaseLab initCustomLab:@"用户名称" color:[UIColor colorWithHex:@"333333"] font:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]];
            
        }
     
        _nameUser.backgroundColor = [UIColor clearColor];

        
//        _nameUser.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
//        _nameUser.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0];
        @weakify(self);
        [[_nameUser addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
    }
    return _nameUser;
}

-(BaseBtn *)supportLab{
    if (!_supportLab) {
        _supportLab = [BaseBtn initCustomBtn:@[@"WXugc_详情_默认"]];
        _supportLab.backgroundColor = [UIColor clearColor];
        
        [[_supportLab addTag]subscribeNext:^(id  _Nullable x) {
            [Tools showTips:@"已赞"];
        }];
    }
    return _supportLab;
}

-(BaseBtn *)unsupportedLab{
    if (!_unsupportedLab) {
        _unsupportedLab = [BaseBtn initCustomBtn:@[@"WX踩"]];
        _unsupportedLab.backgroundColor = [UIColor clearColor];
        
        [[_unsupportedLab addTag]subscribeNext:^(id  _Nullable x) {
            [Tools showTips:@"已踩"];
        }];
    }
    return _unsupportedLab;
}

-(BaseBtn *)followBtn{
    if (!_followBtn) {
        _followBtn = [BaseBtn initCustomBtnTitlestype:@[@"关注"] color:@[[UIColor whiteColor]]];
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
            _followBtn.backgroundColor = Main_Token_Model_MANAGER.backgroundColor;
        }
        _followBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
//        _followBtn.userInteractionEnabled = NO;
        [_followBtn setCornerRadiusWithRadius:4 isMasks:YES];
        [[_followBtn addTag]subscribeNext:^(id  _Nullable x) {
            if (!kUSER_INFO_MANAGER.userIsLogin) {
                [Tools pushSinge];
                return ;
            }
            @weakify(self);
            
            [[serviceDefualt loadAddUserFollowFollowUserId: self.modelComment.userId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                @strongify(self);
 
                if (x.success) {
                    [Tools showTips:@"关注成功"];
                    [self.followBtn setTitle:@"已关注" forState:UIControlStateNormal];
                }else{
//                    if ([x.errorInfo isEqualToString:@"重复记录"]) {
//                        <#statements#>
//                    }
                    [Tools showTips:@"已关注"];
                }
            }];
            
        }];
        

  
            
    }
    return _followBtn;
}

-(BaseBtn *)moreBtn{
    if (!_moreBtn) {
        _moreBtn = [BaseBtn initCustomBtn:@[@"WXugc _更多"]];
        _moreBtn.backgroundColor = [UIColor clearColor];
        _moreBtn.userInteractionEnabled = NO;

    }
    return _moreBtn;
}

-(BaseLab *)tierLab{
    if (!_tierLab) {
        //RGB(245, 245, 245)
        _tierLab = [BaseLab initCustomLab:@"#1" color:HexColor(0x999999) font:[UIFont fontWithName:@"PingFangSC-Semibold" size:14]];
        _tierLab.backgroundColor = [UIColor clearColor];
    }
    return _tierLab;
}
/**
 @property (nonatomic,strong)BaseLab * supportLab;//用户点赞
 @property (nonatomic,strong)BaseLab * unsupportedLab;//用户踩
 @property (nonatomic,strong)BaseBtn * followBtn;//用户关注
 @property (nonatomic,strong)BaseBtn * moreBtn;//用户更多
 
 
 */

-(QMUIButton *)userDelete{
    if (!_userDelete) {
        _userDelete = [[QMUIButton alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userDelete setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _userDelete.titleLabel.font = [UIFont systemFontOfSize:12];
        [_userDelete setTitle:@"删除" forState:UIControlStateNormal];
        _userDelete.hidden = YES;
        @weakify(self);
        [[_userDelete rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [[serviceDefualt loadDelUserCommentCommentId:self.modelComment.commentId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    
                    if (self.index == 0) {
                        Ugc_Detail_VC_WX *vc = (Ugc_Detail_VC_WX*)[self getViewController:[Ugc_Detail_VC_WX class]];
                        //                        if (vc.blockDel) {
                        //                            vc.blockDel();
                        //                        }
                        
                        [vc.tbv.mj_header beginRefreshing];
                        
                    }else{
                        MainFirst_Magazine_VC_WX *vc = (MainFirst_Magazine_VC_WX*)[self getViewController:[MainFirst_Magazine_VC_WX class]];
                        //                        if (vc.blockDel) {
                        //                            vc.blockDel();
                        //                        }
                        [vc.Tbv Starefreshing];
                        
                    }
                    
                    [Tools showTips:@"删除成功"];
                }else{
                    [Tools showTips:x.errorInfo];
                }
            }];
        }];
    }
    return _userDelete;
}





@end
